package com.syn.synold4s.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.graphics.Shader;
import android.util.AttributeSet;

import androidx.core.content.ContextCompat;

import com.gaoyi.basemodule.baseControl.BaseOnlyView;
import com.gaoyi.basemodule.baseUtil.DensityUtil;
import com.syn.synold4s.R;

public class PolygonClipperView extends BaseOnlyView {
    private Paint backgroundPaint;
    private Paint textPaint;
    private String mTitle;
    private float textWidth;

    public PolygonClipperView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void initView(TypedArray ta) {
        mTitle = ta.getString(R.styleable.PolygonClipperView_polygon_clipper_title);
        if (mTitle == null) {
            mTitle = "";
        }
        // 初始化用于绘制背景的Paint
        backgroundPaint = new Paint();
        backgroundPaint.setStyle(Paint.Style.FILL);
        backgroundPaint.setAntiAlias(true);

        // 初始化用于绘制文本的Paint
        textPaint = new Paint();
        textPaint.setTextSize(DensityUtil.getSPValue(mContext,16));  // 设置文本大小
        textPaint.setColor(Color.WHITE);  // 设置文本颜色
        textPaint.setFakeBoldText(true);  // 粗体
        textPaint.setAntiAlias(true);


        // 计算文本宽度
        textWidth = textPaint.measureText(mTitle);
        setLayerType(LAYER_TYPE_HARDWARE, backgroundPaint); // 启用硬件加速
    }

    public void setTitle(String title) {
        if (title == null) {
            mTitle = "";
        }else {
            mTitle = title;
        }
        // 计算文本宽度
        textWidth = textPaint.measureText(mTitle);
        requestLayout();
        invalidate();
    }

    @Override
    protected int[] getAttrs() {
        return R.styleable.PolygonClipperView;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        // 在视图尺寸变化时创建渐变色
        LinearGradient linearGradient = new LinearGradient(0, 0, w, 0,
                ContextCompat.getColor(getContext(), R.color.color_title_start),
                ContextCompat.getColor(getContext(), R.color.color_title_end),
                Shader.TileMode.CLAMP);

        // 将渐变效果设置到背景的Paint上
        backgroundPaint.setShader(linearGradient);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 获取文本的宽度并增加一些padding作为视图的宽度
        int desiredWidth = (int) textWidth + getPaddingLeft() + getPaddingRight() + 40; // 增加额外的宽度以适应padding
        int desiredHeight = (int) (textPaint.getTextSize() + getPaddingTop() + getPaddingBottom() + 20); // 适应文本的高度

        // 使用resolveSize来确定宽度和高度
        int width = resolveSize(desiredWidth, widthMeasureSpec);
        int height = resolveSize(desiredHeight, heightMeasureSpec);

        // 设置测量的宽高
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 创建自定义路径
        Path path = new Path();
        path.lineTo(0, 0);  // 左上角
        path.lineTo(getWidth(), 0);  // 右上角
        path.lineTo(getWidth() - 12, getHeight());  // 右下角稍微偏左
        path.lineTo(0, getHeight());  // 左下角
        path.close();

        // 使用路径剪裁
        canvas.save(); // 保存当前画布状态
        canvas.clipPath(path, Region.Op.INTERSECT);

        // 绘制背景（使用背景的Paint）
        canvas.drawPaint(backgroundPaint);

        // 绘制标题文本（使用文本的Paint）
        float textX = 8;  // 文本的X坐标
        float textY = getHeight() / 2 + (Math.abs(textPaint.descent() + textPaint.ascent()) / 2);  // 居中Y坐标
        canvas.drawText(mTitle, textX, textY, textPaint);
        canvas.restore(); // 恢复画布状态
    }
}
